package com.bh.kysys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataScope;
import com.bh.common.constant.Entity;
import com.bh.common.core.domain.entity.SysUser;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.PageUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.kysys.convert.JcSysOrganizationConvert;
import com.bh.kysys.domain.JcSysOrganization;
import com.bh.kysys.domain.JcSysUserOrganization;
import com.bh.kysys.mapper.JcSysOrganizationMapper;
import com.bh.kysys.service.IJcSysOrganizationService;
import com.bh.kysys.service.IJcSysUserOrganizationService;
import com.bh.kysys.vo.JcSysOrganizationVo;
import com.bh.kysys.vo.JcSysUserOrganizationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * organizationService业务层处理
 *
 * @author slx
 * @date 2023-03-11
 */
@Service
public class JcSysOrganizationServiceImpl extends ServiceImpl<JcSysOrganizationMapper, JcSysOrganization> implements IJcSysOrganizationService {
    @Autowired
    private JcSysOrganizationMapper jcSysOrganizationMapper;

    @Autowired
    private IJcSysUserOrganizationService sysUserOrganizationService;

    @Autowired
    private JcSysOrganizationConvert jcSysOrganizationConvert;
    /**
     * 查询organization
     *
     * @param id organization主键
     * @return organization
     */
    @Override
    public JcSysOrganization selectJcSysOrganizationById(Long id) {
        return jcSysOrganizationMapper.selectById(id);
    }


    /**
     * 查询organization
     *
     * @param name organization名称
     * @return organization
     */
    @Override
    public JcSysOrganization selectJcSysOrganizationByName(String name) {
        return jcSysOrganizationMapper.selectJcSysOrganizationByName(name);
    }

    @Override
    public List<JcSysOrganization> selectUserJcSysOrganizationList(JcSysOrganizationVo jcSysOrganization) {
        return jcSysOrganizationMapper.selectUserJcSysOrganizationList(jcSysOrganization);
    }


    @Override
    public List<JcSysOrganizationVo> selectUserJcSysOrganizationByUserId(JcSysUserOrganizationVo org) {
        List<JcSysOrganization> organizations;
        if (SysUser.isAdmin(org.getUserId())) {
            organizations = jcSysOrganizationMapper.selectUserJcSysOrganizationList(new JcSysOrganizationVo());
            organizations = organizations.stream().filter(o -> !"root".equals(o.getId())).collect(Collectors.toList());
            return jcSysOrganizationConvert.toVoList(organizations);
        }
        List<JcSysUserOrganization> userOrganizations = sysUserOrganizationService.selectJcSysUserOrganizationByUserId(org.getUserId());
        List<Long> orgIds = userOrganizations.stream().map(JcSysUserOrganization::getJcSysOrganizationId).collect(Collectors.toList());
        if (orgIds.size() > 0) {
            JcSysOrganizationVo jcSysOrganizationVo = new JcSysOrganizationVo();
            jcSysOrganizationVo.setIdList(orgIds);
            organizations = jcSysOrganizationMapper.selectUserJcSysOrganizationList(jcSysOrganizationVo);
            return jcSysOrganizationConvert.toVoList(organizations);
        }
        return null;

    }

    @Override
    public List<JcSysOrganization> selectJcSysOrganizationById(String[] ids) {
        return jcSysOrganizationMapper.selectBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<JcSysOrganization> selectJcSysOrganizationByNames(Set<String> names) {
        return jcSysOrganizationMapper.selectJcSysOrganizationByNames(names);
    }

    /**
     * 查询organization列表
     *
     * @param jcSysOrganization organization
     * @return organization
     */
    @Override
    public List<JcSysOrganization> selectJcSysOrganizationList(JcSysOrganization jcSysOrganization) {
        return jcSysOrganizationMapper.selectJcSysOrganizationList(jcSysOrganization);
    }

    /**
     * 查询organization列表
     *
     * @return organization
     */
    @DataScope(deptAlias = "t2")
    @Override
    public List<JcSysOrganization> selectJcSysOrganizationTree(JcSysOrganizationVo jcSysOrganization) {
        jcSysOrganization.setStatus(Entity.ACTIVE);
        return jcSysOrganizationMapper.selectJcSysOrganizationTree(jcSysOrganization);
    }

    @Override
    public List<JcSysOrganization> selectTargetJcSysOrganizationList(JcSysOrganizationVo jcSysOrganization) {
        jcSysOrganization.setStatus(Entity.ACTIVE);
        List<JcSysOrganization> list = jcSysOrganizationMapper.selectJcSysOrganizationTree(jcSysOrganization);
        list = list.stream().filter(o -> !SecurityUtils.getOrgId().equals(o.getId())).collect(Collectors.toList());
        return list;
    }

    /**
     * 新增organization
     *
     * @param jcSysOrganization organization
     * @return 结果
     */
    @CacheEvict(value = "DB_JC_ORG", key = "'*'")
    @Override
    public int insertJcSysOrganization(JcSysOrganization jcSysOrganization) {
        jcSysOrganization.setCreateName(SecurityUtils.getUsername());
        jcSysOrganization.setCreateTime(DateUtils.getNowDate());
        return jcSysOrganizationMapper.insert(jcSysOrganization);
    }

    /**
     * 修改organization
     *
     * @param jcSysOrganization organization
     * @return 结果
     */
    @CacheEvict(value = "DB_JC_ORG", key = "'*'")
    @Override
    public int updateJcSysOrganization(JcSysOrganization jcSysOrganization) {
        jcSysOrganization.setModifyName(SecurityUtils.getUsername());
        jcSysOrganization.setModifyTime(DateUtils.getNowDate());
        return jcSysOrganizationMapper.updateById(jcSysOrganization);
    }

    /**
     * 批量删除organization
     *
     * @param ids 需要删除的organization主键
     * @return 结果
     */
    @Override
    public int deleteJcSysOrganizationByIds(Long[] ids) {
        return jcSysOrganizationMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除organization信息
     *
     * @param id organization主键
     * @return 结果
     */
    @Override
    public int deleteJcSysOrganizationById(Long id) {
        return jcSysOrganizationMapper.deleteById(id);
    }
}
